package com.example.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=GBK");
        PrintWriter out = resp.getWriter();
        out.println("<HTML><BODY><h2>这是一个Get请求</h2></BODY></HTML>");
        out.flush();
        out.close();
        System.out.println("这是一个Get请求");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=GBK");
        PrintWriter out = resp.getWriter();
        out.println("<HTML><BODY><h2>这是一个Post请求</h2></BODY></HTML>");
        out.flush();
        out.close();
        System.out.println("这是一个Post请求");
    }

    @Override
    public void destroy() {
        System.out.println("servlet销毁");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("servlet初始化");
    }
}
